package com.lintesis.lintesismodel.common.hibernate;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;
import java.util.HashMap;

import org.hibernate.SQLQuery;

import com.lintesis.utils.sql.NamedParameterStatement;

public class HashMapToDB
{

	public static void insertHashMap(HashMap<String, Object> hashMap, String dbTable)
	{
		String sqlStatement = "insert into " + dbTable + " ";
		String columnList = "(";
		String valueList = "(";
		for (String key : hashMap.keySet())
		{
			columnList += key + ",";
			valueList += ":" + key + ",";
		}
		columnList = columnList.substring(0, columnList.length() - 1);
		valueList = valueList.substring(0, valueList.length() - 1);
		columnList += ")";
		valueList += ")";
		sqlStatement += columnList + " values " + valueList;
		System.out.println(sqlStatement);
		SQLQuery sqlQuery = ConnectionManager.getSession().createSQLQuery(sqlStatement);
		for (String key : hashMap.keySet())
		{
			setToSqlQuery(sqlQuery, key, hashMap.get(key));
		}
		sqlQuery.executeUpdate();

	}
	
	public static void deleteHashMap(HashMap<String, Object> hashMap, String dbTable)
	{
		String sqlStatement = "delete from " + dbTable + " ";
		String columnList = "(";
		String valueList = "(";
		for (String key : hashMap.keySet())
		{
			columnList += key + ",";
			valueList += ":" + key + ",";
		}
		columnList = columnList.substring(0, columnList.length() - 1);
		valueList = valueList.substring(0, valueList.length() - 1);
		columnList += ")";
		valueList += ")";
		sqlStatement += columnList + " values " + valueList;
		System.out.println(sqlStatement);
		SQLQuery sqlQuery = ConnectionManager.getSession().createSQLQuery(sqlStatement);
		for (String key : hashMap.keySet())
		{
			setToSqlQuery(sqlQuery, key, hashMap.get(key));
		}
		sqlQuery.executeUpdate();

	}

	@SuppressWarnings("deprecation")
	public static HashMap<String, Object> executeQueryToHashMap(String sql, HashMap<String, Object> params) throws Exception
	{
		// PreparedStatement ps =
		// ConnectionManager.getSession().connection().prepareStatement(sql);

		NamedParameterStatement ps = new NamedParameterStatement(ConnectionManager.getSession().connection(), sql);

		for (String key : params.keySet())
		{
			ps.setObject(key, params.get(key));
		}

		ResultSet rs = ps.executeQuery();
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		while (rs.next())
		{
			String key = rs.getString("key");
			Object o = rs.getObject("value");
			hashMap.put(key, o);

		}
		return hashMap;
	}

	@SuppressWarnings("deprecation")
	public static ResultSet executeQuery(String sql, HashMap<String, Object> params) throws Exception
	{
		return executeQuery(sql, params,ConnectionManager.getSession().connection());
	}
	
	@SuppressWarnings("deprecation")
	public static int executeDelete(String sql, HashMap<String, Object> params) throws Exception
	{
		return executeDelete(sql, params,ConnectionManager.getSession().connection());
	}

	@SuppressWarnings("deprecation")
	public static int executeDelete(String sql, HashMap<String, Object> params, Connection connection) throws Exception
	{
		NamedParameterStatement ns = new NamedParameterStatement(ConnectionManager.getSession().connection(), sql);

		for (String key : params.keySet())
		{
			ns.setObject(key, params.get(key));
		}
		
		return ns.executeUpdate();
	}

	
	@SuppressWarnings("deprecation")
	public static ResultSet executeQuery(String sql, HashMap<String, Object> params, Connection connection) throws Exception
	{
		NamedParameterStatement ns = new NamedParameterStatement(ConnectionManager.getSession().connection(), sql);

		for (String key : params.keySet())
		{
			ns.setObject(key, params.get(key));
		}
		
		ResultSet rs = ns.executeQuery();
		return rs;
	}

	@SuppressWarnings("unused")
	private static void setToSqlQuery(SQLQuery sqlQuery, HashMap<String, Object> params)
	{
		if (params == null)
			return;
		for (String key : params.keySet())
		{
			setToSqlQuery(sqlQuery, key, params.get(key));
		}
	}

	private static void setToSqlQuery(SQLQuery sqlQuery, String name, Object value)
	{
		if (value instanceof Integer)
		{
			sqlQuery.setInteger(name, (Integer) value);
			return;
		}
		if (value instanceof Float)
		{
			sqlQuery.setFloat(name, (Float) value);
			return;
		}
		if (value instanceof Date)
		{
			sqlQuery.setTimestamp(name, (Date) value);
			return;
		}
		if (value instanceof Long)
		{
			sqlQuery.setLong(name, (Long) value);
			return;

		}
		if (value instanceof byte[])
		{
			sqlQuery.setBinary(name,(byte[]) value);
			return;
		}
		if(value instanceof String)
		{
			sqlQuery.setString(name,(String)value	);
		}
	}
}
